package servlet.user;


import com.alibaba.fastjson.JSON;
import pojo.Result;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.stream.Collectors;

@WebServlet("/user/update")
@MultipartConfig(fileSizeThreshold = 4 * 1024, maxFileSize = 100000 * 1024)
public class UpdateUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        // 注册用表单传来的数据
//        // 获取文件部分
//        Part productImage = req.getPart("profilePicture");
//        // 获取文件的原始文件名
//        String fileName = productImage.getSubmittedFileName();
//        // 获取文件保存路径（这里使用了相对路径）
//        String pathImage = req.getServletContext().getRealPath("/") + "/static/images/temp/" + fileName;
//        // 将文件保存到指定目录
//        productImage.write(pathImage);

        // 处理其他表单数据（例如：用户名、邮箱、密码等）
        int userId = Integer.parseInt(req.getParameter("userId"));
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String fullName = req.getParameter("fullName");
        String password = req.getParameter("password");
//        User user = new User(username, password, email, fullName, "/static/images/temp/" + fileName, status);
        User user = new User(userId, username,password, email, fullName);
        // 实现注册逻辑
        boolean flag = userService.updateUser(user);
        if (flag) {
            // 注册成功，重定向到登录界面
            resp.sendRedirect("/user/login.jsp");
        } else {
            // 注册失败，输出 HTML 并使用 JavaScript 弹出错误提示
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write("<script>alert('注册失败');</script>");
        }
    }
}
